#include <func.h>

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,3);
    int sockfd;
    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    printf("sockfd=%d\n",sockfd);
    struct sockaddr_in ser;
    bzero(&ser,sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_addr.s_addr=inet_addr(argv[1]);
    ser.sin_port=htons(atoi(argv[2]));
    int ret;
    ret=bind(sockfd,(struct sockaddr*)&ser,sizeof(struct sockaddr));
    ERROR_CHECK(ret,-1,"bind");
    struct sockaddr_in cli;
    bzero(&cli,sizeof(cli));
    char buf[128]={0};
    socklen_t addrlen=sizeof(struct sockaddr);
    recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&cli,&addrlen);
    printf("cli ip=%s,cli port=%d\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
    printf("recv: %s\n",buf);
    bzero(&buf,sizeof(buf));
    read(STDIN_FILENO,buf,sizeof(buf));
    sendto(sockfd,buf,sizeof(buf)-1,0,(struct sockaddr*)&cli,addrlen);
    return 0;
}

